package com.merge;

import com.common.FileWriterUtils;

import java.util.HashMap;
import java.util.Map;

public class FileWrite {

    public static Map<String, Integer> cacheSizeMap = new HashMap<>();

    public static final int MAX_CACHE_SIZE = 10;

    public static void writeLine(String path, int number) {
        if (!cacheSizeMap.containsKey(path)) {
            cacheSizeMap.put(path, 0);
        }
        int size = cacheSizeMap.get(path);
        FileWriterUtils.writeLine(path, number);
        size += 1;
        if (size >= MAX_CACHE_SIZE) {
            FileWriterUtils.flush(path);
            size = 0;
        }
        cacheSizeMap.replace(path, size);
    }

    public static void main(String[] args) {
        String path = "E:\\document\\io_demo\\filewriter_demo.txt";
        for (int i = 1;i <= 99; i++) {
            writeLine(path, i);
        }
    }
}
